package android.homeaccount;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.homeaccount.helper.BigEncode;
import android.homeaccount.helper.DataGetter;
import android.homeaccount.helper.Generic;
import android.homeaccount.helper.Services;
import android.homeaccount.models.M_Login;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Login1 extends Activity
{
	EditText etUsername;
	EditText etPassword;
	
	Button btLogin;
	Button btClear;
	Button btExit;
	
	final static int RESULT_Back = 20;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        //TextView tvUsername = (TextView) findViewById(R.id.tvUsername);
        //TextView tvPassword = (TextView) findViewById(R.id.tvPassword);
        etUsername = (EditText) findViewById(R.id.etUsername);
        etPassword = (EditText) findViewById(R.id.etPassword);
        btLogin = (Button) findViewById(R.id.btLogin);
        btClear = (Button) findViewById(R.id.btClear);
        btExit = (Button) findViewById(R.id.btExit);
        
        btLogin.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if(!isOnline())
				{
					AlertNoNetwork(Login1.this);
				}
				else
				{
					if (!Generic.isValidPassword(etUsername.getText().toString()) || !Generic.isValidPassword(etPassword.getText().toString()))
					{
						etUsername.startAnimation(AnimationUtils.loadAnimation(Login1.this, R.anim.shake));
						etPassword.startAnimation(AnimationUtils.loadAnimation(Login1.this, R.anim.shake));
						
						AlertDialog.Builder dialog = new AlertDialog.Builder(v.getContext());
						dialog.setTitle("Error Massage");
						dialog.setMessage("�?ุณยั�?�?ม�?�?ด�?�?ส�? Username หรือ Password");
						dialog.setNeutralButton("Close", new DialogInterface.OnClickListener()
						{
							@Override
							public void onClick(DialogInterface arg0, int arg1)
							{
								// TODO Auto-generated method stub
							}
						});
						dialog.show();
					}
					else
					{
						new Login().execute();
					}
				}
			}
		});
    }
    
    private class Login extends AsyncTask<String, Void, M_Login>
	{
		private final ProgressDialog dialog = new ProgressDialog(Login1.this);

		protected void onPreExecute()
		{
			this.dialog.setMessage(getString(R.string.s_logging));
			this.dialog.show();
		}

		protected M_Login doInBackground(final String... args)
		{
			Services s = new Services();
			M_Login ln = s.Login(Login1.this.etUsername.getText().toString(),BigEncode.getMD5getBase64(Login1.this.etPassword.getText().toString()));
			return ln;
		}

		// can use UI thread here
		protected void onPostExecute(final M_Login result)
		{	
			if (result != null)
			{
				if (result.token != null)
				{

					DataGetter dg = new DataGetter(Login1.this);
					if (dg.CommitData(Login1.this.etUsername.getText().toString(), result.token))
					{
						if (this.dialog.isShowing())
						{
							this.dialog.dismiss();
						}
						Intent i = new Intent(Login1.this, Menu.class);
						startActivityForResult(i, 0);
					}
					else
					{
						AlertDialog.Builder alertbox = new AlertDialog.Builder(Login1.this);
						alertbox.setMessage(getString(R.string.s_not_success));
						alertbox.setNeutralButton(getString(R.string.s_ok), null);
						alertbox.show();
					}
				}
				else
				{
					AlertCannotGetData(Login1.this);
					Toast.makeText(getApplicationContext(), "asdfasdfasdf", Toast.LENGTH_LONG).show();
				}
			}
			else
			{
				Toast.makeText(getApplicationContext(), "12341234123412341234", Toast.LENGTH_LONG).show();
				AlertCannotGetData(Login1.this);
			}

			if (this.dialog.isShowing())
			{
				this.dialog.dismiss();
			}
		}
	}
    
    public void AlertCannotGetData(Context c)
	{
		AlertDialog.Builder alertbox = new AlertDialog.Builder(c);
		alertbox.setMessage(getString(R.string.s_not_get_data));
		alertbox.setNeutralButton(getString(R.string.s_ok), null);
		alertbox.show();
	}
	
	private Boolean isOnline()
	{
		ConnectivityManager conMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
		if (conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED 
				|| conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED  )
			return true;		
		else return false;
	}
    
    public void AlertNoNetwork(Context c)
	{
		AlertDialog.Builder alertbox = new AlertDialog.Builder(c);
		  alertbox.setMessage(getString(R.string.s_no_network));
		  alertbox.setNeutralButton(getString(R.string.s_ok),null);
		  alertbox.show();		
	}
}